import { Controller } from "../../mvc/core/Controller";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SBuyGroupBuyItem } from "../../../Message/Public/SBuyGroupBuyItem";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { SGroupBuyOpen } from "../../../Message/Public/SGroupBuyOpen";
import { TeamBuyHintButton } from "../view/TeamBuy/TeamBuyHintButton";
import { SGroupBuyInfo } from "../../../Message/Public/SGroupBuyInfo";
import { TeamBuyModule } from "../../../modules/TeamBuyModule";
import { LayerManager } from "../manager/LayerManager";
//class TeamBuyController
    
    export  class TeamBuyController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGroupBuyOpen, this.groupBuyOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGroupBuyInfo, this.serverBackGroupInfos);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGroupBuyClose, this.groupBuyClose);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGroupBuySuccess, this.deelJoinTeamBuyServerBack);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGroupBuyInfoChanged, this.serverHadUpdataHander);
            Dispatcher.addEventListener(EventName.RegistGroupBuy, this.gotoJoinTeamBuyHander);
            return;
        }

        /* internal  */serverHadUpdataHander(arg1: MessageBlock): void
        {
            if (!this._teamBuyModule || this._teamBuyModule.isHide) 
            {
                return;
            }
            this.getTeamBuyMessageFromServer();
            return;
        }

        /* internal  */deelJoinTeamBuyServerBack(arg1: MessageBlock): void
        {
            if (!this._teamBuyModule || this._teamBuyModule.isHide) 
            {
                return;
            }
            var loc1=arg1.messageBase as SBuyGroupBuyItem;
            return;
        }

        /* internal  */gotoJoinTeamBuyHander(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            GameProxy.teamBuyProxy.registGroupBuy(loc1.itemCode, loc1.price, loc1.uint);
            return;
        }

        /* internal  */groupBuyClose(arg1: MessageBlock): void
        {
            this.isOpeng = false;
            if (this._teamBuyModule) 
            {
                if (this._teamBuyModule.isHide == false) 
                {
                    this._teamBuyModule.hide();
                }
            }
            if (this._hintButton) 
            {
                this._hintButton.hide();
            }
            return;
        }

        /* internal  */onRoleLevelUpdateHandler(arg1: Object /* flash.events.Event */): void
        {
            if (cache.role.entityInfo.level >= 60) 
            {
                RolePlayer.instance.removeEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
                if (this.isOpeng) 
                {
                    this.showHintButton();
                }
            }
            return;
        }

        /* internal  */groupBuyOpen(arg1: MessageBlock, arg2: Boolean=false): void
        {
            var loc1=null;
            if (arg1 != null) 
            {
                loc1 = arg1.messageBase as SGroupBuyOpen;
                cache.teamBuyCache.openTime = loc1.openDt;
                cache.teamBuyCache.endTime = loc1.endDt;
            }
            this.isOpeng = true;
            if (arg2 || cache.role.entityInfo.level >= 60) 
            {
                this.showHintButton();
            }
            else 
            {
                RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            }
            return;
        }

        /* internal  */showHintButton(): void
        {
            if (!this._hintButton) 
            {
                this._hintButton = new TeamBuyHintButton();
                this._hintButton.addEventListener(flash.events.MouseEvent.CLICK, this.onHintButtonClick);
            }
            if (this._hintButton.isHide) 
            {
                this._hintButton.show();
            }
            if (this._teamBuyModule && this._teamBuyModule.isHide == false) 
            {
                this._teamBuyModule.updateTimer();
            }
            return;
        }

        /* internal  */serverBackGroupInfos(arg1: MessageBlock): void
        {
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            if (!this._teamBuyModule || this._teamBuyModule.isHide) 
            {
                return;
            }
            var loc1=new Array<any>();
            var loc2=arg1.messageBase as SGroupBuyInfo;
            var loc3=loc2.groupBuy;
            var loc8=0;
            var loc9=loc3;
            for(loc4 of loc9) 
            {
                loc1.push(loc4);
            }
            loc1.sortOn("itemCode", Array.NUMERIC);
            this._teamBuyModule.upDataGroups(loc1);
            loc5 = loc2.MyGroupBuy;
            loc6 = new Array<any>();
            loc8 = 0;
            loc9 = loc5;
            for(loc7 of loc9) 
            {
                loc6.push(loc7);
            }
            loc6.sortOn("itemCode", Array.NUMERIC);
            this._teamBuyModule.upDataMyJointGroups(loc6);
            return;
        }

        /* internal  */onHintButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (!this._teamBuyModule) 
            {
                this._teamBuyModule = new TeamBuyModule(LayerManager.windowLayer);
            }
            this._teamBuyModule.show();
            this.getTeamBuyMessageFromServer();
            this._teamBuyModule.updateTimer();
            return;
        }

        /* internal  */getTeamBuyMessageFromServer(): void
        {
            GameProxy.teamBuyProxy.getGroupBuyInfo();
            return;
        }

        private /* var */_hintButton: TeamBuyHintButton;

        private /* var */_teamBuyModule: TeamBuyModule;

        private /* var */isOpeng: Boolean=false;
    }
